import Vue from 'vue';
import axios from 'axios'
import Cookie from 'js-cookie'
import './axios.config'
const ACCESS_TOKEN = 'access_token';

Vue.prototype.getAjax = function (url,options,needlogin=false) {
    const access_token = Cookie.get(ACCESS_TOKEN);
    if(access_token || !needlogin){
        let opt = {access_token,...options};
        return axios.get(url,{params:opt}).then(res=>{
            return Promise.resolve(res.data);
        });
    }else{
        return Promise.reject({ code: -1, msg: "用户未登录" });
    }
};
Vue.prototype.postAjax = function (url,options,needlogin=true) {
    const access_token = Cookie.get(ACCESS_TOKEN);
    if(access_token || !needlogin){
        let opt = {access_token,...options};
        return axios.post(url, opt).then(res => {
            return Promise.resolve(res.data);
        });
    }else{
        return Promise.reject({ code: -1, msg: "用户未登录" });
    }
};